package com.heima.controller;

import com.heima.pojo.Emp;
import com.heima.pojo.Result;
import com.heima.service.EmpService;
import com.heima.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
@RestController
public class LoginController {
    @Autowired
    EmpService empService;
    @PostMapping("/login")
    public Result login(@RequestBody Emp emp){
        Emp e = empService.login(emp);
        if (e != null){
            HashMap<String, Object> map = new HashMap<>();
            map.put("id",emp.getId());
            map.put("name",emp.getName());
            map.put("username",emp.getUsername());
            String s = JwtUtils.generateJwt(map);
            return Result.success(s);
        }
        return Result.error("用户名或密码错误");
    }
}
